<?php

namespace App\Http\Controllers\Api;

use App\Http\Controllers\Controller;
use App\Http\Requests\Api\RefereeRequest;
use App\Models\Referee;
use App\Http\Resources\RefereeResource;
use App\Models\RefereeBlack;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;
use App\Http\Queries\CustomerInfoQuery;
use App\Http\Resources\CustomerInfoResource;
use App\Models\Dealer;
use App\Http\Queries\RefereeQuery;

class RefereeController extends Controller
{
    public function store(Referee $referee, RefereeRequest $request)
    {
        if (RefereeBlack::phoneExists($request->phone)) {
            return $this->failed('phone-in-blacklist');
        }
        if (!empty($request->dealer_id) && $request->dealer_id != 'undefined') {
            $dealer = Dealer::find($request->dealer_id);
            if (is_null($dealer)) {
                return $this->failed('dealer-invalid', '经销商不存在');
            }
        }
        $user = $request->user();
        $referee->fill($request->all());
        $referee->user_id = $user->id;
        $referee->save();
        return $this->resource(new RefereeResource($referee));
    }

    public function interests()
    {
        $data = config('interest');
        return $this->success($data);
    }

    public function me(Request $request)
    {
        $user = $request->user();
        $referee = $user->rmore;
        if (is_null($referee)) {
            return $this->failed('not-be-referee');
        }
        $rank = app('App\Services\RankService')->setKey('referee-rank');

        $referee->score = $rank->zscore($user->user_id) ?: 0;
        $referee->rank = $rank->zrevrank($user->user_id);
        return $this->resource(new RefereeResource($referee));
    }

    //积分排行榜
    public function rank(Request $request)
    {
        $page = $request->page ? intval($request->page) : 1;
        $num = $request->per_page ? intval($request->per_page) : 10;

        $start = ($page * $num) - $num;
        $end = ($page * $num) - 1;

        $rank = app('App\Services\RankService')->setKey('referee-rank');
        $ids = $rank->zrevrange($start, $end);
        if (!empty($ids)) {
            $ids_ordered = implode(',', $ids);
            $rows = Referee::orderByRaw(DB::raw("FIELD(user_id, $ids_ordered)"))->whereIn('user_id', $ids)->get();
        } else {
            return $this->failed('no-more');
        }
        foreach ($rows as &$row) {
            $row->phone = substr_replace($row->phone, '****', 3, 4);
            $row->score = $rank->zscore($row->user_id);
            $row->rank = $rank->zrevrank($row->user_id);
        }
        return $this->resource(RefereeResource::collection($rows));
    }

    public function records(Request $request, CustomerInfoQuery $query)
    {
        $user = $request->user();
        $rows = $query->where('referee_id', $user->id)->paginate(10);
        foreach ($rows as &$row) {
            $row->phone = substr_replace($row->phone, '****', 3, 4);
        }
        return $this->resource(CustomerInfoResource::collection($rows));
    }

    public function list(RefereeQuery $query)
    {
        $rows = $query->paginate(10);
        return $this->resource(RefereeResource::collection($rows));
    }

    public function all(RefereeQuery $query)
    {
        $rows = $query->get();
        return $this->resource(RefereeResource::collection($rows));
    }

    public function default($id, RefereeQuery $query)
    {
        $customerInfo = $query->findOrFail($id);
        return $this->resource(new RefereeResource($customerInfo));
    }
}
